//
//  LSForgetPasswordViewController.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2018/12/18.
//  Copyright © 2018 wpkj. All rights reserved.
//

#import "LSForgetPasswordViewController.h"

@interface LSForgetPasswordViewController () <UITextFieldDelegate>
@property (nonatomic, strong) UITextField *formerPasswordTF;
@property (nonatomic, strong) UITextField *passwordTF;
@property (nonatomic, strong) UITextField *passwordConfirmTF;
@end

@implementation LSForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"修改密码";
    NSArray *titleArray = @[@"原密码：",@"新密码：",@"确认密码："];
    for (int i = 0; i < 3; i++) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 35*i+10, 80, 35)];
        titleLabel.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:15];
        titleLabel.textColor = hexColor(666666);
        titleLabel.text = titleArray[i];
        [self.view addSubview:titleLabel];
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(20, 35*(i+1)+10, ScreenWidth-40, 0.5)];
        lineView.backgroundColor = hexColor(CCCCCC);
        [self.view addSubview:lineView];
    }
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 125, ScreenWidth-100, 15) andText:@"密码为6-18位字母、数字或者符号组成，至少包含字母和数字" andTextColor:hexColor(666666) andFont:[UIFont fontWithName:@"SourceHanSansCN-Regular" size:8] andTextAlignment:NSTextAlignmentLeft];
    tipLabel.numberOfLines = 2;
    [self.view addSubview:tipLabel];
    [tipLabel sizeToFit];
    UIButton *commitButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 160, ScreenWidth-40, 40)];
    commitButton.layer.cornerRadius = 4;
    commitButton.layer.masksToBounds = true;
    commitButton.backgroundColor = hexColor(ffbc01);
    commitButton.titleLabel.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:15];
    [commitButton setTitle:@"提交" forState:UIControlStateNormal];
    [commitButton setTitleColor:hexColor(f7f7f7) forState:UIControlStateNormal];
    [commitButton addTarget:self action:@selector(commitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitButton];
    
    [self.view addSubview:self.formerPasswordTF];
    [self.view addSubview:self.passwordTF];
    [self.view addSubview:self.passwordConfirmTF];
    self.view.backgroundColor = hexColor(ffffff);
}

- (void)commitButtonClick {
    if ([self.formerPasswordTF.text isEqualToString:@""]) {
        [MBProgressHUD showTipMessageInWindow:@"请输入原密码"];
        return;
    }
    if ([self.passwordTF.text isEqualToString:@""]) {
        [MBProgressHUD showTipMessageInWindow:@"请输入新密码"];
        return;
    }
    if ([self.passwordConfirmTF.text isEqualToString:@""]) {
        [MBProgressHUD showTipMessageInWindow:@"请确认新密码"];
        return;
    }
    if (self.passwordTF.text.length > 18 || self.passwordTF.text.length < 6) {
        [MBProgressHUD showTipMessageInWindow:@"新密码长度不正确"];
        return;
    }
    if (![self.passwordConfirmTF.text isEqualToString:self.passwordTF.text]) {
        [MBProgressHUD showTipMessageInWindow:@"两次密码输入不一致"];
        return;
    }
    int number = [ValidateUtils validateContainNumberAndLetter:self.passwordTF.text];
    if (number != 4 && number != 3) {
        [MBProgressHUD showTipMessageInWindow:@"密码至少包含数字和字母"];
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:true];
    NSDictionary *param = @{@"token":[ValidateUtils validateNull:[UserInfoModel sharedInstance].token],@"id":[ValidateUtils validateNull:[UserInfoModel sharedInstance].customerId],@"password":self.formerPasswordTF.text,@"newpassword":self.passwordTF.text};
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:UserPasswordUpdate param:param completionHandler:^(id responseObject, NSError *connectionError) {
        [hud hideAnimated:true];
        if (!connectionError) {
            NSNumber *status = [responseObject objectForKey:@"status"];
            if (status.integerValue == 200) {
                [MBProgressHUD showTipMessageInWindow:@"修改成功"];
                WS
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakSelf.navigationController popViewControllerAnimated:true];
                });
            } else {
                [MBProgressHUD showTipMessageInWindow:[ValidateUtils validateNull:responseObject[@"message"]]];
            }
        } else {
            [MBProgressHUD showTipMessageInWindow:connectionError.localizedDescription];
        }
    }];
}

//设置文本框只能输入数字，字母和符号
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSUInteger lengthOfString = string.length; //lengthOfString的值始终为1
    for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {
        unichar character = [string characterAtIndex:loopIndex];//将输入的值转化为ASCII值（即内部索引值），可以参考ASCII表
        // 48-57;{0,9};65-90;{A..Z};97-122:{a..z}
        if (character < 33) return NO; // 48 unichar for 0
        if (character > 126) return NO;
    }
    // Check for total length
    NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
    if (proposedNewLength > 18) {
        return NO;//限制长度
    }
    return YES;
}

- (UITextField *)formerPasswordTF {
    if (!_formerPasswordTF) {
        _formerPasswordTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 10, ScreenWidth-150, 35)];
        _formerPasswordTF.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:15];
        _formerPasswordTF.delegate = self;
        _formerPasswordTF.secureTextEntry = true;
    }
    return _formerPasswordTF;
}

- (UITextField *)passwordTF {
    if (!_passwordTF) {
        _passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 35+10, ScreenWidth-150, 35)];
        _passwordTF.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:15];
        _passwordTF.delegate = self;
        _passwordTF.secureTextEntry = true;
    }
    return _passwordTF;
}

- (UITextField *)passwordConfirmTF {
    if (!_passwordConfirmTF) {
        _passwordConfirmTF = [[UITextField alloc] initWithFrame:CGRectMake(100, 35*2+10, ScreenWidth-150, 35)];
        _passwordConfirmTF.font = [UIFont fontWithName:@"SourceHanSansCN-Regular" size:15];
        _passwordConfirmTF.delegate = self;
        _passwordConfirmTF.secureTextEntry = true;
    }
    return _passwordConfirmTF;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
